/*
 * @Description: 这是默认home controller!!!!!!!
 * @Author: Junhuang
 * @Date: 2022-02-08 11:04:34
 * @LastEditTime: 2022-03-22 22:40:20
 * @LastEditors: Junhuang
 * @Reference: 
 */
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    const dataList = {
      list: [
        { id: 1, title: 'this is news 1', url: '/news/1' },
        { id: 2, title: 'this is news 2', url: '/news/2' },
      ],
    };
    //ctx.apiSuccesss([{ id: 1, name: 'dark' }]);
    // 注意这里render的根目录，就是app/view/,完整路径是：app/view/index.tpl
    return await this.ctx.render('index.tpl', dataList);
  }

  async bad() {
    const { ctx } = this;
    return ctx.apiFail('故意错误!!', 'fail', 400);
  }

  // /cate/:cate_id/video/:page?limit=3
  async cate_page() {
    const { ctx, app } = this;
    // 目前validate可能有问题，调用validate后query可能会丢失，
    // 因此如果要使用myquery避免错误，特别是要用到分页，就需要把query先存储一下
    ctx.myquery = ctx.query;
    // 参数验证
    ctx.validate({
      cate_id: {
        type: 'int',
        required: true,
        desc: '分类id'
      },
      page: {
        type: 'int',
        required: true,
        desc: '页码'
      }
    });
    let category_id = ctx.params.cate_id;
    let rows = await ctx.page(app.model.Video, {
      category_id,
    });

    return ctx.apiSuccesss(rows);
  }
}

module.exports = HomeController;
